package com.ruoyi.different.service.impl;

import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.different.mapper.DifferentReteMapper;
import com.ruoyi.different.domain.DifferentRete;
import com.ruoyi.different.service.IDifferentReteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 异网数据Service业务层处理
 * 
 * @author rocky
 * @date 2023-12-13
 */
@Service
public class DifferentReteServiceImpl extends ServiceImpl<DifferentReteMapper, DifferentRete> implements IDifferentReteService
{
    private static final FastDateFormat pattern = FastDateFormat.getInstance("yyyyMMddHHmmss");
    private static final AtomicInteger atomicInteger = new AtomicInteger(1);
    private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<StringBuilder>();
    @Autowired
    private DifferentReteMapper differentReteMapper;

    /**
     * 查询异网数据
     * 
     * @param id 异网数据主键
     * @return 异网数据
     */
    @Override
    //@DataScope(deptAlias = "d", userAlias = "u")
    public DifferentRete selectDifferentReteById(Long id)
    {
        DifferentRete differentRete = differentReteMapper.selectDifferentReteById(id);
        return differentRete;
    }

    /**
     * 查询异网数据列表
     * 
     * @param differentRete 异网数据
     * @return 异网数据
     */
    @Override
    //@DataScope(deptAlias = "d", userAlias = "u")
    public List<DifferentRete> selectDifferentReteList(DifferentRete differentRete)
    {
        return differentReteMapper.selectDifferentReteList(differentRete);
    }

    /**
     * 新增异网数据
     * 
     * @param differentRete 异网数据
     * @return 结果
     */
    @Override
    public Long insertDifferentRete(DifferentRete differentRete)
    {
        /*String lock;
        if (Objects.isNull(threadLocal.get())) {
            lock =  UUID.randomUUID().toString();
            StringBuilder builder = new StringBuilder(pattern.format(Instant.now().toEpochMilli()));// 取系统当前时间作为订单号前半部分
            builder.append(Math.abs(lock.hashCode()));// HASH-CODE
            builder.append(atomicInteger.getAndIncrement());// 自增顺序
            threadLocal.set(builder);
        }
        long aLong = Long.parseLong(threadLocal.get().toString().substring(0,15));*/

        long aLong = IdUtil.getSnowflake(1, 20).nextId();
        String s = String.valueOf(aLong);
        long l = Long.parseLong(s.substring(0, 15));

        differentRete.setCreateTime(DateUtils.getNowDate());
        differentRete.setId(l);
        differentReteMapper.insertDifferentRete(differentRete);
        return l;
    }

    /**
     * 修改异网数据
     * 
     * @param differentRete 异网数据
     * @return 结果
     */
    @Override
    public int updateDifferentRete(DifferentRete differentRete)
    {
        differentRete.setUpdateTime(DateUtils.getNowDate());
        return differentReteMapper.updateDifferentRete(differentRete);
    }

    /**
     * 批量删除异网数据
     * 
     * @param ids 需要删除的异网数据主键
     * @return 结果
     */
    @Override
    public int deleteDifferentReteByIds(Long[] ids)
    {
        return differentReteMapper.deleteDifferentReteByIds(ids);
    }

    /**
     * 删除异网数据信息
     * 
     * @param id 异网数据主键
     * @return 结果
     */
    @Override
    public int deleteDifferentReteById(Long id)
    {
        return differentReteMapper.deleteDifferentReteById(id);
    }

    @Override
    public DifferentRete selectByPhone(DifferentRete differentRete) {
        QueryWrapper<DifferentRete> differentReteQueryWrapper = new QueryWrapper<>();
        differentReteQueryWrapper.lambda().eq(DifferentRete::getTelephone,differentRete.getTelephone());
        List<DifferentRete> list = list(differentReteQueryWrapper);
        if (list.size() > 0) {
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void cheak(DifferentRete differentRete) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SysUser.isAdmin(SecurityUtils.getUserId())&&differentRete.getBranchId()!=Long.valueOf(user.getDeptId()).intValue())
        {
            throw new ServiceException("没有权限访问用户数据！");

        }
    }

    @Override
    public void cheaks(List<DifferentRete> differentRetes) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        for (DifferentRete differentRete : differentRetes) {
            //System.out.println(differentRete.getBranchId());
            //System.out.println(user.getDeptId());
            if (!SysUser.isAdmin(SecurityUtils.getUserId())&&differentRete.getBranchId()!=Long.valueOf(user.getDeptId()).intValue())
            {
                throw new ServiceException("没有权限访问用户数据！");

            }
        }

    }
}
